﻿using MiHoMiao.Mono.UGUI.DraggableSlider;
using UnityEngine;

namespace TodoList.Mono.TaskCreator
{
    public class EnsureButton : MonoBehaviour
    {
        [SerializeField] private DraggableSlider DraggableSlider;
        [SerializeField] private TaskGenerator TaskGenerator;
        
        private float m_ParentWidth;
        private float m_Velocity = 10;
        private bool m_IsEnsured;
        
        private void OnEnable()
        {
            m_IsEnsured = false;
            m_ParentWidth = ((RectTransform)transform.parent).rect.width;
            transform.localPosition = new Vector3(-m_ParentWidth / 2, 0, 0);
            DraggableSlider.HorizontalRange = new Vector2(-m_ParentWidth / 2, m_ParentWidth / 2);
        }

        private float Progress => transform.localPosition.x / m_ParentWidth + 0.5f;

        private void FixedUpdate()
        {
            if (m_IsEnsured) return;
            if (NewMethod()) m_Velocity = 10;
            else
            {
                transform.Translate(-m_Velocity, 0, 0);
                m_Velocity++;
            }
        }

        private bool NewMethod()
        {
            if (Input.GetMouseButton(0)) return true;
            switch (Progress)
            {
                case >= 0.95f:
                    TaskGenerator.Ensure();
                    m_IsEnsured = true;
                    return true;
                case <= 0.0f:
                    return true;
            }

            return false;
        }
    }
}